/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2020年9月29日
 */
package com.massyframework.beanskin;

import com.massyframework.beanskin.license.LicenseBindedEvent;
import com.massyframework.beanskin.license.LicenseChangedEvent;
import com.massyframework.beanskin.license.LicenseToExpireEvent;
import com.massyframework.beanskin.license.LicenseUnbindedEvent;

import com.massyframework.beanskin.assembly.AssemblyEventAdapter;

/**
 * 模块事件适配器提供对模块事件的封装
 * 
 * <p>
 * 如果希望监听装配件事件，请使用{@link AssemblyEventAdapter}
 * 
 * @author huangkh
 *
 */
public abstract class ModuleEventAdapter implements ModuleListener {

	/**
	 * 
	 */
	public ModuleEventAdapter() {
	}
	
	/**
	 * 模块已安装事件处理
	 * @param event {@link ModuleInstalledEvent}
	 */
	protected void onInstalled(ModuleInstalledEvent event) {
		
	}
	
	/**
	 * 模块实例已创建事件
	 * @param event {@link ModuleCreatedEvent}
	 */
	protected void onCreated(ModuleCreatedEvent event) {
		
	}
	
	/**
	 * 模块实例正在销毁事件
	 * @param event {@link ModuleDestoryingEvent}
	 */
	protected void onDestorying(ModuleDestoryingEvent event) {
		
	}
	
	/**
	 * 许可绑定事件
	 * @param event {@link LicenseBindedEvent}
	 */
	protected void onLicenseBindedEvent(LicenseBindedEvent event) {
		
	}
	
	/**
	 * 许可取消绑定事件
	 * @param event {@link LicenseUnbindedEvent}
	 */
	protected void onLicenseUnbindedEvent(LicenseUnbindedEvent event) {
	}
	
	/**
	 * 许可变更事件
	 * @param event {@link LicenseChangedEvent}
	 */
	protected void onLicenseChangedEvent(LicenseChangedEvent event) {
	}
	
	/**
	 * 许可即将到期事件
	 * @param event {@link LicenseToExpireEvent}
	 */
	protected void onLicenseToExpireEvent(LicenseToExpireEvent event) {
		
	}

	@Override
	public final void onEvent(ModuleEvent event) {
		Class<?> eventType = event.getClass();
		this.dispatchEvent(eventType, event);
	}
		
	/**
	 * 事件调度处理
	 * @param eventType {@link Class},事件类型
	 * @param event {@link Event},事件实例
	 */
	protected void dispatchEvent(Class<?> eventType, ModuleEvent event) {
		if (eventType == ModuleCreatedEvent.class) {
			this.onCreated((ModuleCreatedEvent)event);
			return;
		}
		
		if (eventType == ModuleDestoryingEvent.class) {
			this.onDestorying((ModuleDestoryingEvent)event);
			return;
		}
		
		if (eventType == LicenseBindedEvent.class) {
			this.onLicenseBindedEvent((LicenseBindedEvent)event);
		}
		
		if (eventType == LicenseUnbindedEvent.class) {
			this.onLicenseUnbindedEvent((LicenseUnbindedEvent)event);
			return;
		}
		
		if (eventType == LicenseChangedEvent.class) {
			this.onLicenseChangedEvent((LicenseChangedEvent)event);
			return;
		}
		
		if (eventType == LicenseToExpireEvent.class) {
			this.onLicenseToExpireEvent((LicenseToExpireEvent)event);
		}
		
		if (eventType == ModuleInstalledEvent.class) {
			this.onInstalled((ModuleInstalledEvent)event);
			return;
		}
	}

}
